﻿// BookRules.shared.cs
//

using System;
using System.ComponentModel.DataAnnotations;

namespace BookClub.Web.DataModel {

    public static partial class BookRules {

        // Validates that the description field of the book contains meaningful data.
        public static ValidationResult DescriptionIsMeaningful(string description, ValidationContext context) {
            if (String.IsNullOrEmpty(description)) {
                return ValidationResult.Success;
            }

            string[] words = description.Split(new char[] { ' ', '\t', '\n', '\r', },
                                               StringSplitOptions.RemoveEmptyEntries);
            if (words.Length > 5) {
                return ValidationResult.Success;
            }

            return new ValidationResult("The description must be at least a sentence to be meaningful.",
                                        new string[] { "Description" });
        }

        // Validates that the added date of a book follows its publish date, i.e. an example of
        // an entity level rule.
        public static ValidationResult AddedDateFollowsPublishDate(Book book, ValidationContext context) {
            if (book.PublishDate > book.AddedDate) {
                return new ValidationResult("The book must already be published.",
                                            new string[] { "PublishDate" });
            }

            return ValidationResult.Success;
        }
    }
}
